Skip to content

refactor(redirects)!: replace path-to-regexp with built-in linear-time matcher - #632

Open
bkendall wants to merge 1 commit into
mainfrom
pr605_superstatic_update
Open

bkendall wants to merge 1 commit into
mainfrom
pr605_superstatic_update

Conversation

@bkendall

@bkendall bkendall commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Supersedes #605.

Why

Upgrading path-to-regexp from 1.9.0 to 9.0.0 (as in #605) breaks documented Firebase Hosting redirect syntax (:1 numbered regex captures, :post* splats, :value? optional segments, external URL destinations with captures such as https://blog.myapp.com/:post, and ?query destinations). Meanwhile, path-to-regexp@1.9.0 is on an unmaintained 1.x branch and relies on backtracking JavaScript RegExp matching.

Summary

  • Adds src/utils/path-pattern.ts with a zero-dependency tokenizer, iterative memoized NFA matcher (createPathMatcher), and destination template compiler (createPathCompiler):
    • Preserves Firebase Hosting redirect syntax (:name, :1, :name?, :name*, :name+, bare *, optional/partial prefixes, external URLs including explicit :port, and query strings) without Object.prototype lookup.
    • Guarantees linear $O(\text{ops} \times \text{len})$ worst-case matching time, eliminating catastrophic backtracking ReDoS classes (e.g. multi-parameter segments, multiple splats, sequential optional groups).
  • Migrates src/middleware/redirects.ts to use createPathMatcher and createPathCompiler, removing the rawMatch.split("/") workaround and decodeURIComponent(...) round-trip so patterns like source: "/blog/:post*" → destination: "https://blog.myapp.com/:post" and percent-encoded / raw-% destination query strings work cleanly.
  • Removes path-to-regexp (^1.9.0) from package.json and drops path-to-regexp@1.9.0 + isarray@0.0.1 from package-lock.json.
  • Documents breaking edge cases in changelog.txt (undocumented inline regexes inside redirects[].source are no longer supported—use redirects[].regex instead; ambiguous non-. multi-parameter segments like /:a-:b match non-greedily left-to-right).
  • Adds comprehensive unit tests in test/unit/utils/path-pattern.spec.ts (100% coverage, including ReDoS regression tests) and expands test/unit/middleware/redirects.spec.ts.

Verification

  • npm test (eslint + nyc mocha with re2 active): 383 passing, 0 lint errors/warnings (src/utils/path-pattern.ts at 100% statements/branches/functions/lines)
  • npm run build & npm run prepare
  • npm run license:check
  • npm install --package-lock-only --ignore-scripts --registry=https://registry.npmjs.org (idempotent; no lockfile diff)

…e matcher

Replaces direct dependency on path-to-regexp@^1.9.0 with a built-in iterative, memoized NFA matcher and destination compiler in src/utils/path-pattern.ts.

- Preserves Firebase Hosting redirect syntax (:name, :1, :name?, :name*, :name+, bare *, external URLs with ports, and destination query strings) that path-to-regexp v8/v9 broke.
- Guarantees linear O(ops * pathname.length) worst-case matching time, eliminating catastrophic backtracking ReDoS classes.
- Removes path-to-regexp@^1.9.0 (and transitive isarray@0.0.1) from dependencies and package-lock.json.
- Documents breaking edge cases (inline regexes inside redirects[].source and non-greedy left-to-right matching on ambiguous non-dot multi-param segments) in changelog.txt.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces the path-to-regexp dependency with a custom, built-in linear-time redirect path matcher and compiler (path-pattern.ts) to mitigate potential ReDoS vulnerabilities and improve performance. The redirects middleware has been updated to use these new utilities, and comprehensive unit tests have been added to verify the new implementation. No review comments were provided, so there is no additional feedback to address.

@bkendall
bkendall requested a review from joehan December 4, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant